package site.marqstree.latte.ec.main.sort.list;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

import me.yokeyword.fragmentation.SupportHelper;
import site.marqstree.latte.core.delegates.LatteDelegate;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ec.main.sort.SortDelegate;
import site.marqstree.latte.ec.main.sort.content.ContentDelegate;
import site.marqstree.latte.ui.recycler.ItemType;
import site.marqstree.latte.ui.recycler.MultipleFields;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;
import site.marqstree.latte.ui.recycler.MultipleRecyclerAdapter;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.sort.list
 * 文件名: SortRecyclerAdapter
 * 创建者: marqstree
 * 创建时间: 2020/1/29 13:48
 * 描述: TODO
 */
public class SortRecyclerAdapter extends MultipleRecyclerAdapter implements OnItemClickListener {

    private final SortDelegate DELEGATE;
    private int mCurrentContentId;
    private int mPrePosition = 0;
    private List<MultipleItemEntity> mData;

    protected SortRecyclerAdapter(List<MultipleItemEntity> data, SortDelegate delegate, int contentId) {
        super(data);
        this.DELEGATE = delegate;
        this.mData = data;
        //添加垂直菜单布局
        addItemType(ItemType.VERTICAL_MENU_LIST, R.layout.item_vertical_menu_list);

        //绑定点击Item事件
        this.setOnItemClickListener(this);

        this.mCurrentContentId = contentId;
        showContent();

    }

    @Override
    protected void convert(final BaseViewHolder holder, final MultipleItemEntity entity) {
        super.convert(holder, entity);
        switch (holder.getItemViewType()) {
            case ItemType.VERTICAL_MENU_LIST:
                //获取文字内容
                final String text = entity.getField(MultipleFields.TEXT);
                //是否选中
                final boolean isClicked = entity.getField(MultipleFields.TAG);
                //获取文字控件
                final TextView name = holder.getView(R.id.tv_vertical_item_name);
                //获取线条控件
                final View line = holder.getView(R.id.view_line);
                //获取RecyclerView的Item
                final View itemView = holder.itemView;

                //设置未选中Item的字体、线条颜色
                if (!isClicked) {
                    //隐藏线条
                    line.setVisibility(View.INVISIBLE);
                    //恢复黑色文字
                    name.setTextColor(ContextCompat.getColor(getContext(), R.color.we_chat_black));
                    //设置背景色
                    itemView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.item_background));
                }
                //设置选中Item的字体、线条颜色
                else {
                    //显示线条
                    line.setVisibility(View.VISIBLE);
                    //高亮文字
                    name.setTextColor(ContextCompat.getColor(getContext(), R.color.app_main));
                    //高亮线条
                    line.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.app_main));
                    //高亮背景
                    itemView.setBackgroundColor(Color.WHITE);
                }

                //设置文字内容
                holder.setText(R.id.tv_vertical_item_name, text);

                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        //获取选中行号
        final int currentPosition = position;
        MultipleItemEntity entity = mData.get(position);
        View itemView = view;

        if (mPrePosition != currentPosition) {
            //还原上一个
            getData().get(mPrePosition).setField(MultipleFields.TAG, false);
            notifyItemChanged(mPrePosition);

            //更新选中的item
            entity.setField(MultipleFields.TAG, true);
            notifyItemChanged(currentPosition);
            mPrePosition = currentPosition;

            mCurrentContentId = getData().get(currentPosition).getField(MultipleFields.ID);
            showContent();
        }
    }

    private void showContent() {
        final ContentDelegate delegate = ContentDelegate.newInstance(mCurrentContentId);
        switchContent(delegate);
    }

    private void switchContent(ContentDelegate delegate) {
        //获取右侧的内容子Fragment
        final LatteDelegate contentDelegate =
                SupportHelper.findFragment(DELEGATE.getChildFragmentManager(), ContentDelegate.class);
        if (contentDelegate != null) {
            //replaceFragment:显隐Fragment
            //第一个参数：隐藏的Fragment
            //第二个参数：是否加入Fragment栈
            contentDelegate.getSupportDelegate().replaceFragment(delegate, false);
        }
    }
}
